package cn.nkjobsearch.convert;

import java.sql.*;
import java.util.ArrayList;

import cn.nkjobsearch.mysql.MysqlConn;
import cn.nkjobsearch.publicClass.SearchAndSort;

/**
 * 倒排索引相关的类
 * */
public class KeywordIndex 
{
	public static final int keywordNum = 154;
	public String keywordName[] = new String[keywordNum];
	public int keywordIdInt[] = new int[keywordNum];
	public int keywordCount[] = new int[keywordNum];
	private MysqlConn mysql = null;
	private ResultSet rs = null;
	
	public KeywordIndex(MysqlConn m)
	{
		mysql = m;
		getKeyword();
	}
	
	public boolean putKeywordt2KeywordIndex(String jobinfoId,String title, String jobIntro)
	{
		int id = Integer.parseInt(jobinfoId);
		jobIntro = jobIntro.toLowerCase();
		title = title.toLowerCase();
		for(int i = 0;i<keywordNum;i++)
		{
			if(title.indexOf(keywordName[i]) != -1)
			{
				title = title.replaceAll(keywordName[i], "");
				jobIntro = jobIntro.replaceAll(keywordName[i], "");
				mysql.executeInsert("insert into keywordindex(jobinfoId,keywordId,isTitle) values ('"+id+"','"+keywordIdInt[i]+"','1');");
			} else if(jobIntro.indexOf(keywordName[i]) != -1) {
				jobIntro = jobIntro.replaceAll(keywordName[i], "");
				mysql.executeInsert("insert into keywordindex(jobinfoId,keywordId,isTitle) values ('"+id+"','"+keywordIdInt[i]+"','0');");
			}
		}
		return true;
	}
	
	/**
	 * @param keywordFromUI 用户输入的查询语句<br/>
	 * 与词库匹配后返回所属词库中词汇的id号
	 * */
	public int[] getKeywordFromUI(String keywordFromUI) {
		ArrayList <int[]> keywordList = new ArrayList<int[]>();
		keywordFromUI = keywordFromUI.toLowerCase();
		for(int i = 0;i<keywordNum;i++) {
			int indexOfKeywordFromUI = keywordFromUI.indexOf(keywordName[i]);
			if( indexOfKeywordFromUI != -1 ) {
				String replacement = "";
				for(int j=0; j<keywordName[i].length(); j++) {
					replacement += "?";
				}
				keywordFromUI = keywordFromUI.replaceAll(keywordName[i], replacement);
				keywordList.add(new int[]{keywordIdInt[i], indexOfKeywordFromUI});
			}
		}
		if(keywordList.size() == 0) {
			return null;
		}
		int[] idSorted = new int[keywordList.size()];
		int[] index = new int[keywordList.size()];
		for(int i=0; i<keywordList.size(); i++) {
			idSorted[i] = keywordList.get(i)[0];
			index[i] = keywordList.get(i)[1];
		}
		SearchAndSort.QuickSort(index, idSorted, 0, index.length - 1, true);
		return idSorted;
	}
	
	private void getKeyword() {
		rs = mysql.executeQuery("select id,name,rank from keyword where `count` != '0' order by name desc;");
		int i = 0;
		try {
			while (rs.next()) 
			{
				keywordIdInt[i] = rs.getInt("id");
				keywordName[i] = rs.getString("name").toLowerCase();
				keywordCount[i] = rs.getInt("rank");
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("There are "+i+" keywords.");
//		for(int j=0; j<i; j++) {
//			System.out.println(j + "\t" + keywordIdInt[j] + "\t" + keywordName[j] + "\t"
//					+ keywordCount[j]);
//		}
	}
}
